// Copyright 2007 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.enterprise.connector.gdata;

import com.google.enterprise.connector.spi.ConnectorType;
import com.google.enterprise.connector.spi.ConfigureResponse;

import java.util.Map;
import java.util.ResourceBundle;
import java.util.Locale;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.net.URL;
import java.net.MalformedURLException;

/**
 * This class serves as the ConnectorType for the GData connector.  Primarily,
 * it provides a means of input and validation for a single property called
 * "feedUrl" which should be a valid URL to be fetched during normal connector
 * operation.
 * 
 * @author amsmith@google.com (Adam Smith)
 */
public class GdConnectorType implements ConnectorType {

  /** The logger for this class. */
  private static final Logger LOGGER =
      Logger.getLogger(GdConnectorType.class.getName());
  
  /** This is the name of the input used in the HTML form. */ 
  public static final String FEEDURL_KEY = "feedUrl";
  
  /** {@inheritDoc} */
  public ConfigureResponse getConfigForm(Locale locale) {
    return new ConfigureResponse("", makeForm(null, locale));    
  }
  
  /** {@inheritDoc} */
  public ConfigureResponse getPopulatedConfigForm(Map configMap,
      Locale locale) {
    return new ConfigureResponse("", makeForm(configMap, locale));
  }
  
  /**
   * {@inheritDoc}
   */
  public ConfigureResponse validateConfig(Map configMap, Locale locale) {
    if (isValid(configMap)) {
      return null;
    } else {
      return new ConfigureResponse(getString("invalid_feed_url", locale),
          makeForm(configMap, locale));
    }
  }
  
  private static String makeForm(Map configMap, Locale locale) {
    String feedUrl = null;
    if (configMap != null)
      feedUrl = (String) configMap.get(FEEDURL_KEY);
    if (feedUrl == null)
      feedUrl = "";
    StringBuffer buffer = new StringBuffer(256)
      .append("<tr>\n")
      .append("\t<td>")
      .append(getString("tips_label", locale))
      .append("\t</td>\n")
      .append("\t<td>")
      .append(getString("tips_text", locale))
      .append("\t</td>\n")
      .append("</tr>\n")
      .append("<tr>\n")
      .append("\t<td>")
      .append(getString("feed_url_label", locale))
      .append("</td>\n")
      .append("\t<td>\n")
      .append("\t\t<input type=\"text\" name=\"")
      .append(FEEDURL_KEY)
      .append("\" size=\"72\" value=\"")
      .append(feedUrl)
      .append("\" />\n")
      .append("\t</td>\n")
      .append("</tr>\n");

    return buffer.toString();
  }
  
  /**
   * This method only checks that the mapped feedUrl is a valid URL string,
   * NOT that it references a reachable resource or that it contains any GData
   * content.
   */
  private static boolean isValid(Map configMap) {
    String feedUrl = (String) configMap.get(FEEDURL_KEY);
    URL url = null;
    try {
      url = new URL(feedUrl); // make sure it parses correctly
    } catch (MalformedURLException murle) {
      if (LOGGER.isLoggable(Level.INFO))
        LOGGER.info("Malformed feed URL: " + feedUrl);
    }
    return url != null;
  }
  
  /** Gets the localized string for a given key. */
  private static String getString(String key, Locale locale) {
    ResourceBundle rb = ResourceBundle.getBundle("GdResources", locale);
    return rb.getString(key);
  }
}
